
// 轮播图部分
let items = document.getElementsByClassName("item");
let points = document.getElementsByClassName("point");
let leftBtn = document.getElementById("btn-left");
let rightBtn = document.getElementById("btn-right");
let content = document.querySelector(".content");
let index = 0;
let timer = null;

// 清楚class
let clearActive = function() {
  for(let i = 0; i < items.length; i++) {
    items[i].className = "item";
    points[i].className = "point";
  }
}

let goIndex = function() {
  clearActive();
  items[index].className = "item active";
  points[index].className = "point active";
}

rightBtn.onclick = function() {
  if (index < items.length-1) {
    index++;
  } else {
    index = 0;
  }
  goIndex();
}

leftBtn.onclick = function() {
  if (index > 0) {
    index--;
  } else {
    index = items.length-1;
  }
  goIndex();
}

// 开启
timer = setInterval(()=>{
  rightBtn.onclick();
},1500)

// 当鼠标移入时候，关闭定时器
content.onmouseover = function() {
  clearInterval(timer);
}

// 当鼠标移出时 重新开启计时器
content.onmouseout = function() {
  timer = setInterval(()=>{
    rightBtn.onclick();
  },1500);
}

// 当点击原圆点时 跳转到 对应图片
for(let i = 0; i < points.length; i++) {
  points[i].addEventListener("click", function() {
    // 给所有的圆点添加监听事件
    let pointIndex = this.getAttribute("data-index");
    index = pointIndex;
    goIndex();
  })
}



// 此部分不用管
var searchInputs = document.getElementsByClassName("header-search")[0].getElementsByTagName("input");
searchInputs[0].onmousedown = function(){
  searchInputs[1].style.border = "1px solid rgb(255, 106, 0)";
}
searchInputs[0].onblur = function(){
  searchInputs[1].style.border = "1px solid rgb(224, 224, 224)";
}






// 主页小米秒杀倒计时
var list_limit = document.getElementById("list-limit");
var fastLTimeSpans = list_limit.getElementsByTagName("div");
var seckill = new Date('2022-11-1 15:52:30');
let now = new Date();
var times = (seckill - now) / 1000;
var lastDay = document.getElementById("lastDay");
var hours = 0;
var minutes = 0;
var seconds = 0;
setInterval(function () {
  times--;
  day = Math.floor(times / 60 / 60 / 24);
  hours = Math.floor((times / 60 / 60 ) % 24);
  minutes = Math.floor((times / 60) % 60);
  seconds = Math.floor(times % 60);
  hours = hours >= 10 ? hours : "0" + hours;
  minutes = minutes >= 10 ? minutes : "0" + minutes;
  seconds = seconds >= 10 ? seconds : "0" + seconds;
  lastDay.innerText = "本场还剩：" + day + "天";
  console.log(lastDay.innerText);
  fastLTimeSpans[0].innerHTML = hours;
  fastLTimeSpans[1].innerHTML = minutes;
  fastLTimeSpans[2].innerHTML = seconds;
},1000);



function getProductInfo(productId){
  // alert(productId)
  window.location.href = "/mi/cart/add_cart?productId=" + productId;
}



